<?php

namespace app\logic\model\group;

use app\admin\model\user\UserModel;
use app\api\model\group\GroupGoodsApiModel;
use app\logic\model\LogicModel;
use think\model\concern\SoftDelete;

class GroupOrderLogicModel extends LogicModel
{

    use SoftDelete;

    protected $name='group_order';
    protected $delete_time = 'delete_time';
    public function goods(){
        return $this->hasOne(GroupGoodsApiModel::class,'id','goods_id');
    }
    public function isBuy($id,$buyer_id){
        return (bool)$this->where('goods_id', $id)
            ->where('buyer_id', $buyer_id)
            ->where('status', '>', 10)
            ->value('id');
    }
    public static function getBuyNum($goods_id): int
    {
        return self::where('goods_id', $goods_id)
            ->where('status', '>', 10)
            ->count();
    }
    public static function getApiBuyNum($goods,$goods_id = 0)
    {
        if(empty($goods)){
            $goods = GroupGoodsApiModel::where('id',$goods)->find();
            $goods_id = $goods['id'];
        }
        $order_buy_num = self::getBuyNum($goods['id']);
        $sum_num = $goods['num'] + $order_buy_num;
        if( $sum_num > $goods['complete_num']){
            return $goods['complete_num'];
        }else{
            return $sum_num;
        }
    }
    public static function getAdminBuyNum($goods,$goods_id = 0)
    {
        if(empty($goods)){
            $goods = GroupGoodsApiModel::where('id',$goods)->find();
            $goods_id = $goods['id'];
        }
        $order_buy_num = self::getBuyNum($goods_id);
        return $goods['num'] + $order_buy_num;
    }
}